package com.ai.zuochengyun.phase01.class03;

public class Code06_RemoveNode {
    public static class Node {
        int value;
        Node next;
        public Node(int value) {
            this.value = value;
        }
        public Node next(Node next) {
            this.next = next;
            return this;
        }
    }

    public Node removeNode(Node head, int num) {
        if (head == null) {
            return null;
        }
        // 可能有换头的情况
        while (head != null){
            if (head.value != num) {
                break;
            }
            head = head.next;
        }

        Node cur = head;
        Node pre = head;
        while (cur != null){
            if (cur.value == num) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}
